﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright company="Collaboris Ltd." file="Macro.cs">
//   Copyright (c) Collaboris Ltd. All rights Reserved.
// </copyright>
// <summary>
//   action macro.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Collaboris.Saf.Engine
{
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.IO;
    using Spring.Context.Support;
    using Spring.Objects.Factory.Config;

    using Tracing;

    using Utils;

    /*
    /// <summary>
    /// action macro.
    /// </summary>
    public class Macro_old : MacroBase_old
    {
        #region Fields (3) 

        /// <summary>
        /// alias file path.
        /// </summary>
        private static readonly string aliasFilePath = Globals.CollaborisConfigPath + "aliases.xml";

        /// <summary>
        /// alias resource.
        /// </summary>
        private static readonly string aliasResource = "file://" + aliasFilePath;

        /// <summary>
        /// macro object name.
        /// </summary>
        private const string macroObjectName = "MacroToProcess";

        
        public static TraceSwitch TraceSwitch
        {
            get
            {
                return SafTraceHelper.TraceSwitch;
            }
        }

        #endregion Fields 

        #region Constructors (1) 
        
        /// <summary>
        /// Initializes a new instance of the <see cref="Macro"/> class.
        /// </summary>
        /// <param name="actions">
        /// The actions.
        /// </param>
        /// <param name="id">
        /// The id.
        /// </param>
        public Macro(IList<IAction> actions, Guid id) : base(actions, id)
        {
            foreach (IAction action in actions)
            {
                action.Macro = this;
            }
        }
        
        /// <summary>
        /// Initializes a new instance of the <see cref="Macro"/> class.
        /// </summary>
        /// <param name="actions">
        /// The actions.
        /// </param>
        /// <param name="id">
        /// The id.
        /// </param>
        public Macro(List<string> actionSequence, Guid id)
            : base(actionSequence, id)
        {
           
        }

        #endregion Constructors 

        #region Methods (5) 

        //  Public Methods (4) 

         /// <summary>
        /// create.
        /// </summary>
        /// <param name="configLocations">
        /// The config Locations.
        /// </param>
        /// <param name="ctx">
        /// The ctx.
        /// </param>
        /// <returns>
        /// </returns>
        public static Macro Create(List<string> configLocations, SafContext ctx)
        {
             return Create(configLocations, ctx, null);
        }

        /// <summary>
        /// create.
        /// </summary>
        /// <param name="configLocations">The config Locations.</param>
        /// <param name="ctx">The ctx.</param>
        /// <param name="userProps">The user props.</param>
        /// <returns></returns>
        public static Macro Create(List<string> configLocations, 
                                   SafContext ctx, 
                                   Dictionary<string,object> userProps)
        {
            TraceHelper.TraceMethodStart(TraceSwitch, "Macro", "Create");
            
            if (!File.Exists(aliasFilePath))
            {
                TraceHelper.TraceError(TraceSwitch, "Resources.Logging.AliasesFileNotBeFound", aliasFilePath);
                
                throw new FileNotFoundException();
            }

            configLocations.Add(aliasResource);
            
            XmlApplicationContext context = CreateXmlContext(configLocations, ctx);

            // Creates the macro and registers the Variable Place Holder
            Macro macro = CreateMacro(ctx, context);

            TraceHelper.TraceMethodEnd(TraceSwitch, "Macro", "Create");

            return macro;
        }

        /// <summary>
        /// create.
        /// </summary>
        /// <param name="configLocation">The config location.</param>
        /// <param name="ctx">The ctx.</param>
        /// <returns></returns>
        public static Macro Create(string configLocation, SafContext ctx)
        {
            TraceHelper.TraceMethodStart(TraceSwitch, "Macro", "Create");
            
            List<string> configLocations = new List<string>();
            configLocations.Add(configLocation);

            Macro macro = Create(configLocations, ctx);

            TraceHelper.TraceMethodEnd(TraceSwitch, "Macro", "Create");
            
            return macro;
        }

        public static Macro CreateFromFile(string macroPath, SafContext ctx)
        {
            return CreateFromFile(macroPath, ctx, null);
        }
        
        /// <summary>
        /// Create Macro from a path to a file
        /// </summary>
        /// <param name="macroPath">The full path to the macro.</param>
        /// <param name="ctx">The ctx.</param>
        /// <param name="aliasPath">The path to a Spring.Net config file containing Aliases.</param>
        /// <returns></returns>
        public static Macro CreateFromFile(string macroPath, SafContext ctx, string aliasPath)
        {
            List<string> aliases = GetAliasesFromDir();
            aliases.Add(macroPath);

            if (!string.IsNullOrEmpty(aliasPath))
            {
                aliases.Add("file://" + aliasPath);
            }

            XmlApplicationContext context = CreateXmlContext(aliases, ctx);

            // Creates the macro and registers the Variable Place Holder
            Macro macro = CreateMacro(ctx, context);
            TraceHelper.TraceMethodEnd(TraceSwitch, "Macro", "CreateFromFile");

            return macro;
        }

        /// <summary>
        /// Creates the Spring Context for the macro
        /// </summary>
        /// <param name="macroPath">
        /// The macro path.
        /// </param>
        /// <param name="aliasPath">
        /// The alias path.
        /// </param>
        /// <param name="ctx">
        /// The ctx.
        /// </param>
        /// <returns>
        /// </returns>
        private static XmlApplicationContext CreateXmlContext(List<string> configLocations, SafContext ctx)
        {
            TraceHelper.TraceMethodStart(TraceSwitch, "Macro", "CreateXmlContext");

            XmlApplicationContext context = new XmlApplicationContext(false, null, true, null, configLocations.ToArray());
            context.AddObjectFactoryPostProcessor(AddPlaceHolderConfigurer(ctx));

            context.Refresh();

            TraceHelper.TraceMethodEnd(TraceSwitch, "Macro", "CreateXmlContext");
            return context;
        }

        /// <summary>
        /// Gets Name.
        /// </summary>
        public override string Name
        {
            get { return "Collaboris.Saf.Engine.Macro"; }
        }
       
        private string macroFileName;


        /// <summary>
        /// Gets or sets the aliases top pass into Spring.Net
        /// </summary>
        /// <value>The aliases.</value>
        private static List<string> GetAliasesFromDir()
        {
            List<string> list = new List<string>();
            string[] files = Directory.GetFiles(Globals.CollaborisConfigPath, "saf.*.xml");
            
            list.AddRange(files);
            if (files.Length > 0)
            {
                foreach (string file in files)
                {
                    list.Add("file://" + file);
                }
            }

            // now add the standard collaboris alias in.
            list.Add(aliasResource);
            return list;
        }

        /// <summary>
        /// create macro.
        /// </summary>
        /// <param name="ctx">The CTX.</param>
        /// <param name="context">The context.</param>
        /// <returns></returns>
        private static Macro CreateMacro(SafContext ctx, XmlApplicationContext context)
        {
            TraceHelper.TraceMethodStart(TraceSwitch, "Macro", "CreateMacro");
           
            // create macro from Factory
            Macro macro = (Macro)context.GetObject(macroObjectName);
            macro.SpringContext = context;
            
            TraceHelper.TraceMethodEnd(TraceSwitch, "Macro", "CreateMacro");

            return macro;
        }

        /// <summary>
        /// Gets the VariablePlaceholderCnfigurer object.
        /// </summary>
        /// <param name="ctx">The CTX.</param>
        /// <param name="context">The context.</param>
        private static VariablePlaceholderConfigurer AddPlaceHolderConfigurer(SafContext ctx)
        {
            TraceHelper.TraceMethodStart(TraceSwitch, "Macro", "AddPlaceHolderConfigurer");
            VariablePlaceholderConfigurer placeholderConfigurer = new VariablePlaceholderConfigurer();
            placeholderConfigurer.IgnoreUnresolvablePlaceholders = true;
            placeholderConfigurer.VariableSources = GetConfigurerSources(ctx);

            TraceHelper.TraceMethodEnd(TraceSwitch, "Macro", "AddPlaceHolderConfigurer");

            return placeholderConfigurer;
        }

        /// <summary>
        /// Gets the configurer sources.
        /// </summary>
        /// <param name="ctx">The CTX.</param>
        /// <returns></returns>
        private static ArrayList GetConfigurerSources(SafContext ctx)
        {
            TraceHelper.TraceMethodStart(TraceSwitch, "Macro", "GetConfigurerSources");
            
            ArrayList listOfSources = new ArrayList();

            // Current Context.
            listOfSources.Add(ctx);

            // Config
            listOfSources.Add(new ConfigSectionVariableSource("appSettings"));

            // Environment Variables
            listOfSources.Add(new EnvironmentVariableSource());

            // Command Line Args
            listOfSources.Add(new CommandLineArgsVariableSource());

            // RegistryVariableSource
            listOfSources.Add(new RegistryVariableSource());

            TraceHelper.TraceMethodEnd(TraceSwitch, "Macro", "GetConfigurerSources");
            
            return listOfSources;
        }

        #endregion Methods 
    } */   
}
